package com.chunshen.weibo.weibodemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.util.Log;
import android.widget.ImageView;

import com.chunshen.weibo.weibodemo.R;
import com.chunshen.weibo.weibodemo.fragment.HomeFragment;
import com.chunshen.weibo.weibodemo.tools.SPUtils;
import com.chunshen.weibo.weibodemo.common.BaseApplication;
import com.chunshen.weibo.weibodemo.common.Constant;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.exception.WeiboException;

/**
 * Created by yanxiaoqiang on 2016/6/18.
 */

public class LoadingActivity extends BaseActivity {
    private SsoHandler mSsoHandler;
    private AuthInfo mAuthInfo;
    private SPUtils mSPUtils;
    private ImageView ivLanding;
    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.loading_layout);
        //Log.e("LoadingActivity","");
        //Todo隐藏toolbar
        hideToolBar();
        mAuthInfo = new AuthInfo(getApplicationContext(), Constant.APP_KEY, Constant.REDIRECT_URL, Constant
                .SCOPE);
        mSsoHandler = new SsoHandler(this,mAuthInfo);
        mSPUtils = SPUtils.getInstance(getApplicationContext());
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                //check user is login
//                if(checkIsLogin()){
//                    startActivity(new Intent(BaseApplication.getApplicationContextx(), HomeFragment.class));
//                }
//            }
//        },500);
        initialize();
    }

    private void initialize() {
        ivLanding = (ImageView) findViewById(R.id.ivLanding);
    }

    //检查用户是否登录
    private boolean checkIsLogin() {
        if(mSPUtils.isLogin()){
            startActivity(new Intent(LoadingActivity.this, HomePageActivity.class));
            finish();
        }else {
            mSsoHandler.authorize(new WeiboAuthListener() {
                @Override
                public void onComplete(Bundle bundle) {
                    Oauth2AccessToken token= Oauth2AccessToken.parseAccessToken(bundle);
                    mSPUtils.saveToken(token);
                    startActivity(new Intent(LoadingActivity.this, HomePageActivity.class));
                    finish();
                }

                @Override
                public void onWeiboException(WeiboException e) {

                }

                @Override
                public void onCancel() {

                }
            });
        }
        return false;
    }

    @Override
    public int getLayoutId() {
        return R.layout.loading_layout;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(null!=mSsoHandler){
            mSsoHandler.authorizeCallBack(requestCode,resultCode,data);
        }
    }

}
